<template>
  <div class="container">
    <!-- 1.顶部盒子 -->
    <div class="info-box">
      <van-image
        round
        fit="cover"
        width="64"
        height="64"
        :src="userInfo.photo"
      />
      <h2 class="name">
        {{ userInfo.name }}
        <br />
        <van-tag type="primary" color="#fff" text-color="#3296fa">
          {{ userInfo.birthday }}
        </van-tag>
      </h2>
    </div>
    <!-- 用户操作连接 -->
    <van-row>
      <van-col span="8">
        <van-icon name="newspaper-o" color="#7af" size="30" />
        <span>我的作品</span>
      </van-col>
      <van-col span="8">
        <van-icon name="star-o" color="#f00" size="30" />
        <span>我的收藏</span>
      </van-col>
      <van-col span="8">
        <van-icon name="tosend" color="#fa0" size="30" />
        <span>阅读历史</span>
      </van-col>
    </van-row>
    <!-- 用户操作cell -->
    <van-cell-group>
      <van-cell
        title="编辑资料"
        icon="edit"
        is-link
        @click="$router.push({ path: '/edit' })"
      />
      <van-cell title="小智同学" icon="chat-o" is-link @click="$router.push('/student')"/>
      <van-cell title="系统设置" icon="setting-o" is-link />
      <van-cell title="退出登录" icon="warning-o" is-link @click="logout" />
    </van-cell-group>
  </div>
</template>

<script>
// 导入接口
import { getUserInfo } from "./user";
// 导入token.js
import { removeToken } from "@/utils/token.js";
export default {
  name: "user",
  data() {
    return {
      userInfo: {},
    };
  },
  created() {
    // 请求个人详细信息
    getUserInfo().then((res) => {
      console.log(res);
      // 2.绑定数据
      this.userInfo = res.data.data;
      // 3.数据更新到vuex中
      this.$store.commit("updateUserInfo", res.data.data);
    });
  },
  methods: {
    // 退出登录
    logout() {
      this.$dialog
        .confirm({
          title: "提示",
          message: "确定要退出登录吗?",
        })
        .then(() => {
          // 1.清除token
          removeToken();
          //2. 清除userInfo
          this.userInfo = {};
          // 3.跳转登录页
          this.$router.push({ path: "/login" });
        })
        .catch(() => {
          //  取消退出登录
        });
    },
  },
};
</script>

<style lang="less">
.container {
  /* 头部 */
  .info-box {
    height: 100px;
    background-color: #3296fa;
    display: flex;
    align-items: center;
    padding-left: 15px;
    .name {
      font-size: 18px;
      margin-left: 15px;
      color: white;
      font-weight: normal;
    }
  }
  .van-row {
    text-align: center;
    margin: 10px 0;
    span {
      font-size: 12px;
      display: block;
    }
  }
}
</style>